#include "titlebar.h"
#include <QHBoxLayout>
#include <QPainter>
#include <QFile>
#include <QMouseEvent>
#include <QApplication>
#include "custombox.h"
#include <QButtonGroup>

TitleBar::TitleBar(QWidget *parent)
    : QWidget(parent)
{
    AtIcon = new QLabel();
    AtTitleContent = new QLabel();
    AtButtonClose = new QPushButton();

    AtTitleContent->setObjectName("TitleContent");
    AtButtonClose->setFixedSize(QSize(24, 24));
    AtButtonClose->setObjectName("ButtonClose");

    QHBoxLayout* BoxLayout = new QHBoxLayout(this);
    BoxLayout->addWidget(AtIcon);
    BoxLayout->addWidget(AtTitleContent);
    BoxLayout->addWidget(AtButtonClose);

    BoxLayout->setContentsMargins(5, 0, 5, 0);
    BoxLayout->setSpacing(0);

    AtTitleContent->setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Fixed);

    AtButtonClose->setFocusPolicy(Qt::NoFocus);

    //设置标题栏图标
    SetTitleIcon(":/Resources/Title.png", QSize(20, 20));
    //设置标题栏文字
    AtTitleContent->setText(QStringLiteral("温馨提示"));

    AtButtonClose->setCursor(Qt::PointingHandCursor);

    setFixedHeight(36);
    setWindowFlags(Qt::FramelessWindowHint);

    QObject::connect(AtButtonClose, SIGNAL(clicked()), this, SLOT(OnBoxButtonCloseClicked()));

    setStyleSheet(
        "QLabel#TitleContent{color:yellow;padding-left:3px;font:12pt'Microsoft YaHei';}"
        "QPushButton#ButtonClose{border-image:url(:/Resources/Close1.png);border-top-right-radius:3;}"
        "QPushButton#ButtonClose:hover{border-image:url(:/Resources/Close3.png);border-top-right-radius:3;}"
        "QPushButton#ButtonClose:pressed{border-image:url(:/Resources/Close2.png);border-top-right-radius:3;}"
    );
}

TitleBar::~TitleBar() {}

//设置标题栏图标
void TitleBar::SetTitleIcon(QString ResPath, QSize IconSize)
{
    QPixmap titleIcon(ResPath);
    AtIcon->setPixmap(titleIcon.scaled(IconSize));
}

//设置标题栏长度
void TitleBar::SetTitleWidth(int Width)
{
    this->setFixedWidth(Width);
}

void TitleBar::OnBoxButtonCloseClicked()
{
    emit sButtonCloseClicked();
}
